package com.ruoyi.controller;

import com.aliyun.oss.OSSClient;
import com.ruoyi.entity.Classes;
import com.ruoyi.service.CourseService;
import com.ruoyi.utils.Result;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

@RestController
@RequestMapping("/course")
public class CourseController {

    @Value("${server.port}")
    String port;
    //bucket
    @Value("${oss.bucket}")
    String bucket;

    //hostName
    @Value("${oss.hostName}")
    String hostName;

    @Resource
    private OSSClient ossClient;
    @Resource
    private CourseService courseService;

    @RequestMapping("/courseList")
    public Result courseList(){

        return courseService.courseList();
    }

    @RequestMapping("/courseSave")
    public Result courseSave(@RequestBody Classes classes){

        return courseService.courseSave(classes);
    }

    @RequestMapping("/courseDel")
    public Result courseDel(@RequestParam("id") Integer id){

        return courseService.courseDel(id);
    }

    @RequestMapping("/courseOss")
    public Result courseOss(@RequestParam("file") MultipartFile file) {
        try {
            //OSS
            String fileName =  file.getOriginalFilename();
            ossClient.putObject(bucket,fileName,file.getInputStream());
            return Result.success(hostName+fileName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Result.error(20002,"上传失败!");
    }
}
